package com.layui.ad.config.shiro;

import com.layui.ad.config.shiro.entiry.ShiroUser;
import com.layui.ad.entity.SysAdmin;
import com.layui.ad.entity.SysMenu;
import com.layui.ad.service.SysAdminService;
import com.layui.ad.service.SysMenuService;
import com.layui.ad.utils.ShiroUtils;
import com.layui.ad.vo.SysMenuVo;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


public class MyShiroRealm extends AuthorizingRealm {

    @Resource
    private SysAdminService gmSysAdminService;

    @Autowired
    private SysMenuService sysMenuService;

    public MyShiroRealm() {
        setName("MyShiroRealm");
        // 采用MD5加密
//        setCredentialsMatcher(new HashedCredentialsMatcher("md5"));
    }

    /**
     * 用来查询用户的角色和权限
     * 此方法的执行条件：
     * 对权限或者角色进行校验的时候，会主动触发此方法的执行
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("菜单权限：----------------");
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        Integer adminId = ShiroUtils.getAdminId();
        if(adminId == 1){
            simpleAuthorizationInfo.addStringPermission("*:*");
        }else{
            Set<String> permissions = new HashSet<String>();
            List<SysMenu> menuList = sysMenuService.getMenuListByAdminId(adminId);
            for (SysMenu menu : menuList) {
                if (StringUtils.isEmpty(menu.getPermission())) {
                    continue;
                }
                permissions.add(menu.getPermission());
                System.out.println("菜单权限：" + menu.getPermission());
            }
            simpleAuthorizationInfo.addStringPermissions(permissions);

        }

        return simpleAuthorizationInfo;
    }

    /**
     * 查询并提供用户信息
     * 参数：传递进来的用户名和密码
     * 返回值：
     * 如果登录成功将用户的信息封装到AuthenticationInfo中
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken Atoken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) Atoken;
        String username = token.getUsername();
        String password = new String(token.getPassword());
        SysAdmin data = gmSysAdminService.findUsername(username);
//      ByteSource.Util.bytes(tbBlogger.getSalt())

        ShiroUser shiroUser = new ShiroUser();
        shiroUser.setRealName(data.getRealname());
        shiroUser.setUserId(data.getId());
//        shiroUser.setCityId(data.getCityId());
        SimpleAuthenticationInfo aInfo = new SimpleAuthenticationInfo(shiroUser, data.getPassword(), getName());

        // 当验证都通过后，把用户信息放在session里
//        Session session = SecurityUtils.getSubject().getSession();
//        session.setAttribute("userSession", data);
//        session.setAttribute("userSessionId", data.getId());

        return aInfo;
    }


}